<template>
	<view class="container">
		<view class="midBox" style="margin-top: 0;">
			<view class="list flex-ac">
				<view class="hd">时间</view>
				<view class="md" @click="show = true">
					<input type="text" v-model="params.oilTime" disabled placeholder="请选择加油时间"  placeholder-style="color:#999">
				</view>
				<view class="bd">
					<u-icon name="arrow-right" size="24" style="margin-left: 16rpx;" color="#999"></u-icon>
				</view>
			</view>
			<u-picker mode="time" v-model="show" title="选择加油时间" :params="paramsTime" @confirm="addFuelTime" ></u-picker>
			<!-- <view class="list flex-ac">
				<view class="hd">类型</view>
				<view class="md">
					<input type="text" v-model="carTypeName" placeholder="请选择" placeholder-style="color:#999" disabled>
				</view>
				<view class="bd">
					<u-icon name="arrow-right" size="24" style="margin-left: 16rpx;" color="#999"></u-icon>
				</view>
			</view> -->
			<u-picker mode="selector" v-model="show1"  :default-selector="[0]" :range="selector" @confirm="selCarType"></u-picker>
			<!-- <view class="list flex-ac">
				<view class="hd">车牌号</view>
				<view class="md">
					<input type="text" v-model="params.licensePlateNumber" placeholder="请输入" disabled placeholder-style="color:#999">
				</view>
				<view class="bd">
					<u-icon name="arrow-right" size="24" color="#999"></u-icon>
				</view>
			</view> -->
			<view class="list flex-ac">
				<view class="hd">加油量</view>
				<view class="md">
					<input type="digit" v-model="params.oilNum" placeholder="请输入"  placeholder-style="color:#999">
				</view>
				<view class="bd" style="color: #333;">（L）</view>
			</view>
			<view class="list flex-ac">
				<view class="hd">单价</view>
				<view class="md">
					<input type="digit" v-model="params.price" placeholder="请输入"  placeholder-style="color:#999">
				</view>
				<view class="bd" style="color: #333;">（元/升）</view>
			</view>
			<view class="list flex-ac">
				<view class="hd">加油金额</view>
				<view class="md">
					<input type="digit" v-model="params.money" placeholder="请输入"  placeholder-style="color:#999">
				</view>
				<view class="bd" style="color: #333;">（元）</view>
			</view>
			<view class="list flex-ac">
				<view class="hd">尿素</view>
				<view class="md">
					<input type="digit" v-model="params.urea" placeholder="请输入"  placeholder-style="color:#999">
				</view>
				<view class="bd" style="color: #333;">（L）</view>
			</view>
			<view class="list flex-ac">
				<view class="hd">当前里程数</view>
				<view class="md">
					<input type="digit" v-model="params.mileage" placeholder="请输入"  placeholder-style="color:#999">
				</view>
				<view class="bd" style="color: #333;">（km）</view>
			</view>
		</view>
		<view class="midBox">
			<view class="image-tit">里程表图片（最多3张）</view>
			<view class="imageBox flex-ac">
				<view class="item" v-for="(item,index) in mileageUrls" :key="index">
					<image class="full" :src="item" mode="aspectFit"></image>
					<image src="/static/img/del.png" class="del" @click="delMileImage(index)" mode=""></image>
				</view>
				<view class="item" @click="uploadMileImage">
					<image src="/static/img/add-icon.png" mode="aspectFill" class="full"></image>
				</view>
			</view>
			<view class="image-tit">加油图片（最多3张）</view>
			<view class="imageBox flex-ac">
				<view class="item" v-for="(item,index) in oilConsumUrls" :key="index">
					<image class="full":src="item" mode="aspectFit"></image>
					<image src="/static/img/del.png" class="del" @click="delFuelImage(index)" mode=""></image>
				</view>
				<view class="item"  @click="uploadFuelImage">
					<image src="/static/img/add-icon.png" mode="aspectFill" class="full"></image>
				</view>
			</view>
		</view>
		<view class="midBox">
			<view class="image-tit">备注</view>
			<view class="textBox">
				<view class="text">
					<textarea name="" v-model="params.remark" placeholder="请输入备注内容" placeholder-style="color:#CCCCCC" id="" cols="30" rows="10"></textarea>
				</view>
			</view>
		</view>
		
		<view class="foot">
			<view class="btnBox flex-ac">
				<view class="btn-1 middle" @click="sureEdit">确定</view>
			</view>
			<x-botSpace></x-botSpace>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				botDis: this.$app.globalData.botDis,
				show: false,
				show1: false,
				type: 1, //1新增  2编辑
				paramsTime: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				selector: [ '在外加油', '自有加油',],
				params:{
					id: '',
					mileage:"", // 当前里程数
					mileageUrls:"", // 里程表图片
					money:"", // 加油金额
					oilConsumUrls:"", // 加油图片
					oilNum:"", // 加油量
					oilTime:"",  // 加油时间
					price:"",  // 加油单价
					urea:"", // 尿素
					remark:"", // 备注
				},
				carNumber:"",
				danjia:'',
				carTypeName:'',
				mileageUrls:[],
				oilConsumUrls:[],
				id: ''
			}
		},
		onLoad(opt) {
			if(opt) console.log('opt:', opt)
			if(opt.id){
				this.params.id = opt.id
				this.id = opt.id
				this.type = 2
				uni.setNavigationBarTitle({
					title: '编辑'
				})
				this.getInfo(opt.id)
			}
		},
		computed:{
			addFuelAmount(){
				if(this.params.addFuelVolume&&this.danjia){
					return parseFloat(this.params.addFuelVolume) * parseFloat(this.danjia)
				}
			}
		},
		methods: {
			// 确认添加
			sureAdd(){
				this.params.addFuelAmount = this.addFuelAmount
				this.params.addFuelImgs = this.addFuelImgsId.toString()
				this.params.odometerImgs = this.mileImgsId.toString()
				if(this.params.refuelTime === '') return this.$Toast.message('请完善信息')
				if(this.params.addFuelVolume === '') return this.$Toast.message('请完善信息')
				if(this.params.addFuelAmount === '') return this.$Toast.message('请完善信息')
				if(this.params.currentMileage === '') return this.$Toast.message('请完善信息')
				if(this.params.addFuelImgs === '') return this.$Toast.message('请完善信息')
				if(this.params.odometerImgs === '') return this.$Toast.message('请完善信息')
				this.$Net.post('/driverApi/refuelManage/add', this.params).then(res=>{
					console.log('res:', res)
					this.$Toast.messageIcon('添加成功')
					setTimeout(()=>{
						this.$Jump.Back(1)
					}, 1500)
				})
			},
			sureEdit(){
				this.params.oilConsumUrls = this.oilConsumUrls.toString()
				this.params.mileageUrls = this.mileageUrls.toString()
				if(this.params.mileage === '') return this.$Toast.message('请完善信息')
				if(this.params.mileageUrls === '') return this.$Toast.message('请完善信息')
				if(this.params.money === '') return this.$Toast.message('请完善信息')
				if(this.params.oilConsumUrls === '') return this.$Toast.message('请完善信息')
				if(this.params.oilNum === '') return this.$Toast.message('请完善信息')
				if(this.params.oilTime === '') return this.$Toast.message('请完善信息')
				if(this.params.price === '') return this.$Toast.message('请完善信息')
				this.$Net.post('/carRefueling/edit', this.params).then(res=>{
					console.log('res:', res)
					this.$Toast.messageIcon('修改成功')
					setTimeout(()=>{
						this.$Jump.Back(1)
					}, 1500)
				})
			},
			// 上传图片
			uploadMileImage(){
				this.$Net.uploadFile('/sysFileInfo/upload').then(res=>{
					console.log('图片上传:', res)
					this.mileImgs.push(res.data.fileUrl)
					this.mileImgsId.push(res.data.fileId)
				})
			},
			uploadFuelImage(){
				this.$Net.uploadFile('/sysFileInfo/upload').then(res=>{
					console.log('图片上传:', res)
					this.addFuelImgs.push(res.data.fileUrl)
					this.addFuelImgsId.push(res.data.fileId)
				})
			},
			// 删除图片
			delMileImage(index){
				this.mileageUrls.splice(index, 1)
			},
			delFuelImage(index){
				this.oilConsumUrls.splice(index, 1)
			},
			
			addFuelTime(e){
				this.params.oilTime = e.year + "-" + e.month + '-' + e.day + ' ' + e.hour + ":" + e.minute
			},
			selCarType(e){
				console.log(e)
				this.carTypeName = this.selector[e[0]]
				this.params.vehicleType = e[0] + 1
			},
			// 获取加油详情
			getInfo(id){
				this.$Net.get('/carRefueling/detail/'+ this.id).then(res=>{
					this.params.mileage = res.data.mileage
					this.params.mileageUrls = res.data.mileageUrls
					if(res.data.mileageUrls) this.mileageUrls = res.data.mileageUrls.split(',')
					this.params.money = res.data.money
					this.params.oilConsumUrls = res.data.oilConsumUrls
					if(res.data.oilConsumUrls) this.oilConsumUrls = res.data.oilConsumUrls.split(',')
					this.params.oilNum = res.data.oilNum
					this.params.oilTime = res.data.oilTime
					this.params.price = res.data.price
					this.params.remark = res.data.remark
					this.params.urea = res.data.urea
					this.isLoading = true
				})
			},
			// 获取车辆信息
			getCarInfo(){
				this.$Net.get('/driverApi/vehicle/info').then(res=>{
					console.log('车辆信息:', res.data)
					this.params.vehicleId = res.data.vehicleId
					this.params.licensePlateNumber = res.data.licensePlate
					this.params.vehicleType = res.data.vehicleType
					this.carTypeName = res.data.vehicleType == 1 ? '自有车辆' : res.data.vehicleType == 2 ? '专线车辆' : '外调车辆'
					
				})
			},
			// 获取司机信息
			getDriverInfo(){
				this.$Net.get('/driverApi/driver/info').then(res=>{
					console.log('司机信息:', res.data)
					this.params.driverId = res.data.driverId
					this.params.driverMobile = res.data.driverMobile
					this.params.driverName = res.data.driverName
					this.params.driverType = res.data.driverType
				})
			}
		}
	}
</script>

<style lang="scss">
	page{ background-color: #f9f9f9; }
</style>

<style lang="scss" scoped>
	.foot {
		width: 100%;
		padding: 30rpx;
		padding-top: 60rpx;
		.btnBox {
			width: 100%;
	
			.btn-1 {
				flex: 1;
				height: 90rpx;
				background: #1684FC;
				border-radius: 10rpx;
				color: #ffffff;
				font-size: 34rpx;
			}
	
			.btn-2 {
				flex: 1;
				height: 90rpx;
				border: 1px solid #1684FC;
				border-radius: 10rpx;
				color: #1684FC;
				font-size: 34rpx;
			}
		}
	}
	.midBox{
		width: 100%;
		margin-top: 20rpx;
		background-color: #fff;
		.image-tit{
			padding-left: 40rpx;
			padding-top: 30rpx;
			color: #666;
		}
		.textBox{
			width: 100%;
			margin-top: 32rpx;
			padding: 0 24rpx;
			padding-bottom: 32rpx;
			.text{
				border-radius: 8rpx;
				padding: 16rpx;
				width: 100%;
				height: 248rpx;
				background-color: #F6F6F6;
				textarea{
					width: 100%;
					height: 100%;
				}
			}
		}
		.imageBox{
			padding: 30rpx 40rpx 24rpx;
			.item{
				width: 160rpx;
				height: 160rpx;
				margin-right: 30rpx;
				position: relative;
				.del{
					width: 32rpx;
					height: 32rpx;
					position: absolute;
					top: -12rpx;
					right: -12rpx;
					z-index: 10;
				}
			}
		}
		.list{
			width: 100%;
			height: 96rpx;
			border-bottom: 1rpx solid #e5e5e5;
			.hd{
				width: 234rpx;
				padding-left: 40rpx;
				color: #333;
			}
			.md{
				flex: 1;
				height: 100%;
				input{
					width: 100%;
					height: 100%;
					line-height: 94rpx;
					text-align: right;
				}
			}
			.bd{
				padding-right: 24rpx;
				color: #999;
				image{
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
		.list:last-child{
			border: none;
		}
	}
	.head{
		width: 100%;
		height: 288rpx;
		background-color: #fff;
		.list{
			width: 100%;
			height: 96rpx;
			.hd{
				width: 234rpx;
				padding-left: 40rpx;
				color: #999;
			}
			.bd{
				flex: 1;
			}
		}
	}
	// @import './style.scss';
</style>
